package com.handmark.pulltorefresh.library;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;

import com.bytedance.common.utility.UIUtils;
import com.handmark.pulltorefresh.library.internal.EmptyViewMethodAccessor;

public abstract class PullToRefreshAdapterViewBase<T extends AbsListView> extends PullToRefreshBase<T> implements AbsListView.OnScrollListener {
    private AbsListView.OnScrollListener mOnScrollListener;
    private a g;
    private View h;
    private boolean i;

    public PullToRefreshAdapterViewBase(Context arg2) {
        super(arg2);
        this.i = true;
        this.mRefreshableView.setOnScrollListener(((AbsListView.OnScrollListener) this));
    }

    public PullToRefreshAdapterViewBase(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.i = true;
        this.mRefreshableView.setOnScrollListener(((AbsListView.OnScrollListener) this));
    }

    public PullToRefreshAdapterViewBase(Context arg2, Mode arg3) {
        super(arg2, arg3);
        this.i = true;
        this.mRefreshableView.setOnScrollListener(((AbsListView.OnScrollListener) this));
    }

    public PullToRefreshAdapterViewBase(Context arg2, Mode arg3, AnimationStyle arg4) {
        super(arg2, arg3, arg4);
        this.i = true;
        this.mRefreshableView.setOnScrollListener(this);
    }

    public final boolean isReadyForPullStart() {
        boolean v0_2;
        Adapter v0 = this.mRefreshableView.getAdapter();
        if (v0 == null || (v0.isEmpty())) {
            v0_2 = true;
        } else {
            if (this.mRefreshableView.getFirstVisiblePosition() <= 1) {
                View v0_1 = this.mRefreshableView.getChildAt(0);
                if (v0_1 != null) {
                    v0_2 = v0_1.getTop() >= this.mRefreshableView.getTop() ? true : false;
                    return v0_2;
                }
            }

            v0_2 = false;
        }

        return v0_2;
    }

    public final boolean isReadyForPullEnd() {
        boolean v0_2;
        Adapter v0 = this.mRefreshableView.getAdapter();
        if (v0 == null || (v0.isEmpty())) {
            v0_2 = true;
        } else {
            int v3 = this.mRefreshableView.getCount() - 1;
            int v4 = this.mRefreshableView.getLastVisiblePosition();
            if (v4 >= v3 - 1) {
                View v0_1 = this.mRefreshableView.getChildAt(v4 - this.mRefreshableView.getFirstVisiblePosition());
                if (v0_1 != null) {
                    v0_2 = v0_1.getBottom() <= this.mRefreshableView.getBottom() ? true : false;
                    return v0_2;
                }
            }

            v0_2 = false;
        }

        return v0_2;
    }

    public final void onScroll(AbsListView arg2, int arg3, int arg4, int arg5) {
        if (this.mOnScrollListener != null) {
            this.mOnScrollListener.onScroll(arg2, arg3, arg4, arg5);
        }
    }

    protected void onScrollChanged(int arg4, int arg5, int arg6, int arg7) {
        super.onScrollChanged(arg4, arg5, arg6, arg7);
        if (this.h != null && !this.i) {
            this.h.scrollTo(-arg4, -arg5);
        }

        if (this.g != null) {
            this.g.onViewScrollChanged(arg4, arg5, arg6, arg7);
        }
    }

    public final void onScrollStateChanged(AbsListView arg2, int arg3) {
        if (this.mOnScrollListener != null) {
            this.mOnScrollListener.onScrollStateChanged(arg2, arg3);
        }
    }

    public void setAdapter(ListAdapter arg2) {
        this.mRefreshableView.setAdapter(arg2);
    }

    public final void setEmptyView(View arg6) {
        LinearLayout.LayoutParams v0_2;
        int v4 = -1;
        if (arg6 != null) {
            arg6.setClickable(true);
            int v2 = UIUtils.b(this.mRefreshableView);
            if (v2 >= 0) {
                ViewParent v0 = arg6.getParent();
                if (v0 != null && ((v0 instanceof ViewGroup))) {
                    ((ViewGroup) v0).removeView(arg6);
                }

                ViewGroup.LayoutParams v0_1 = arg6.getLayoutParams();
                LinearLayout.LayoutParams v1 = null;
                if (v0_1 != null) {
                    v1 = new LinearLayout.LayoutParams(v0_1);
                    if ((v0_1 instanceof LinearLayout.LayoutParams)) {
                        v1.gravity = ((LinearLayout.LayoutParams) v0_1).gravity;
                        v0_2 = v1;
                    } else {
                        v1.gravity = 17;
                        v0_2 = v1;
                    }
                } else {
                    v0_2 = v1;
                }

                if (v0_2 != null) {
                    this.addViewInternal(arg6, v2, ((ViewGroup.LayoutParams) v0_2));
//                    goto label_24;
                } else {
                    this.addViewInternal(arg6, v2, new LinearLayout.LayoutParams(v4, v4));
                }
            }
        }

        label_24:
        if ((this.mRefreshableView instanceof EmptyViewMethodAccessor)) {
            ((EmptyViewMethodAccessor) this.mRefreshableView).setEmptyViewInternal(arg6);
        } else {
            this.mRefreshableView.setEmptyView(arg6);
        }

        this.h = arg6;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener arg2) {
        this.mRefreshableView.setOnItemClickListener(arg2);
    }

    public final void setOnScrollListener(AbsListView.OnScrollListener arg1) {
        this.mOnScrollListener = arg1;
    }

    public final void setOnViewScrollListener(a arg1) {
        this.g = arg1;
    }

    public final void setScrollEmptyView(boolean arg1) {
        this.i = arg1;
    }

    public interface a {
        void onViewScrollChanged(int arg1, int arg2, int arg3, int arg4);
    }
}

